package com.qs.questionnairesurveysystem.service.impl;

import com.qs.questionnairesurveysystem.entity.Admin;
import com.qs.questionnairesurveysystem.entity.User;
import com.qs.questionnairesurveysystem.mapper.UserMapper;
import com.qs.questionnairesurveysystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Long userId) {
        return userMapper.findById(userId);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.findAll();
    }

    @Override
    public int createUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(Long userId) {
        return userMapper.deleteUser(userId);
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    // 模拟验证用户的身份
    public Admin authenticateUser(String username, String password) {
        // 这里你可以根据用户名和密码查数据库，以下是一个简单的例子
        if ("admin".equals(username) && "password".equals(password)) {
            return new Admin("admin", "password");
        }
        return null;
    }

    // 加载用户
    public Admin loadUserByUsername(String username) {
        // 在真实的实现中，你需要从数据库中加载用户信息
        return new Admin(username, "password");
    }
}
